﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using Backend.Data.Entities;

namespace Backend.Data.Mappings
{
    class GeoReferenciaMap : EntityTypeConfiguration<GeoReferencia>
    {
        public GeoReferenciaMap()
        {
            //table
            ToTable("GeoReferencias");

            //pk
            HasKey(t => t.Id);

            //property
            Property(t => t.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
            //relationship
            // 1 to 0.1, no permite null: una GeoReferencia pertenece a (o es) un LegalEntity
            HasRequired(t => t.LegalEntity)
                .WithRequiredDependent(c => c.GeoReferencia)
                .WillCascadeOnDelete(false);
            /*
            // si fuese 0.1 to 0.1 permite null 
            HasOptional(t => t.LegalEntity)
                .WithOptionalDependent(c => c.GeoReferencia)
                .WillCascadeOnDelete(false);
            */

            // 0.1 to 0.* permite null: puede o no tener un Parent
            HasOptional(x => x.Parent)
               .WithMany(x => x.GeoReferencias)
               .HasForeignKey(x => x.ParentId)
               .WillCascadeOnDelete(false);


        }
    }
}
